package com.appspot.didit.client.tag;

import java.util.ArrayList;

import com.appspot.didit.client.ui.EnhancedTextBox;
import com.appspot.didit.client.ui.LabelWithLink;
import com.appspot.didit.client.util.Util;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.DecoratedPopupPanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.TextBox;

public class TagCloud extends DecoratedPopupPanel {
	
	FlowPanel fp;
	TextBox tb;
	KeyUpHandler kuh;
	HandlerRegistration hr; 
	ArrayList<LabelWithLink> lwl;
	
	public TagCloud() {
		super(true, false);
		fp = new FlowPanel();
		for(int i =1; i<100;i++) 
			fp.add(new HTML("test"));
 		setWidget(fp);
 		setStylePrimaryName("gwt-SuggestBoxPopup");
 		setPreviewingAllNativeEvents(true);	
	}
	
	public TagCloud(Tag[] tags) {
		super(true, false);
		fp = new FlowPanel();
		lwl = new ArrayList<LabelWithLink>();
		for(int i = 0; i < tags.length; i++) {
			Tag t = tags[i];
			LabelWithLink l = new LabelWithLink(t.getTagname(), Util.getTagSize(t));
			lwl.add(l);
			fp.add(l);
			final LabelWithLink lb = l;
			l.addClickHandler(new ClickHandler() {
				@SuppressWarnings("unchecked")
				public void onClick(ClickEvent event) {
					//show(tb);
					String value = tb.getValue();
					String value2 = lb.getText();
					if(!(value.startsWith(value2 + " ") || value.contains(" " + value2 + " ") || value.endsWith(" " + value2) || value.equals(value2))) {
						tb.setValue(value + (value.length()>0?" ":"") + value2);
						lb.setState(1);
					} else {
						String[] tags = Util.splitStringInArray(value);
						String value3 = "";
						for(int i = 0; i < tags.length; i++) {
							if(!tags[i].equals(value2)) {
								value3 = value3 + " " + tags[i] ;
							}
						}
						tb.setValue(value3.equals("")?"":value3.substring(1));
						lb.setState(0);
					}
					if(tb instanceof EnhancedTextBox) {
						((EnhancedTextBox)tb).update();
					}
					ValueChangeEvent.fire(tb, tb.getValue());
					updateTagLabelState(tb.getValue());
				}
			});
		}
		setWidget(fp);
 		setStyleName("gwt-DecoratedPopupPanel");
 		setPreviewingAllNativeEvents(true);	
	}
	
	public void addPartner(TextBox tb) {
		addAutoHidePartner(tb.getElement());
	}
	
	public void show(TextBox tb1) {
		/******************************
		 * FIXME: Bug at Focus Change
		 */
		if(this.tb != null) {
			hr.removeHandler();
			this.removeAutoHidePartner(tb.getElement());
		}
		this.tb = tb1;
		addAutoHidePartner(tb.getElement());
		updateTagLabelState(tb.getValue());
		kuh = new KeyUpHandler() {
			public void onKeyUp(KeyUpEvent event) {
				updateTagLabelState(tb.getValue());
			}
 		};
 		hr = tb1.addKeyUpHandler(kuh);
 		
 		show();
 		if(isShowing()) {
			System.out.println("showing!");
		}
 		showRelativeTo(tb);
	}
	
	private void updateTagLabelState(String value) {
		for(int i = 0; i<lwl.size(); i++) {
			String value2 = lwl.get(i).getText();
			if(value.startsWith(value2 + " ") || value.contains(" " + value2 + " ") || value.endsWith(" " + value2) || value.equals(value2)) {
				lwl.get(i).setState(1);
			} else {
				lwl.get(i).setState(0);
			}
		}
		
	}
	public void hide() {
		super.hide();
		hr.removeHandler();
		this.removeAutoHidePartner(tb.getElement());
		tb = null;
	}
	
	public void updateTag(Tag t) {
		for(LabelWithLink l : lwl) {
			if(t.getTagname().equals(l.getText())) {
				l.setFontSize(Util.getTagSize(t));
				return;
			}
		}
	}
	
	public void addTag(Tag t) {
		LabelWithLink l = new LabelWithLink(t.getTagname(), Util.getTagSize(t));
		lwl.add(l);
		fp.add(l);
		final LabelWithLink lb = l;
		l.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				//show(tb);
				String value = tb.getValue();
				String value2 = lb.getText();
				if(!(value.startsWith(value2 + " ") || value.contains(" " + value2 + " ") || value.endsWith(" " + value2) || value.equals(value2))) {
					tb.setValue(value + (value.length()>0?" ":"") + value2);
					lb.setState(1);
				} else {
					String[] tags = Util.splitStringInArray(value);
					String value3 = "";
					for(int i = 0; i < tags.length; i++) {
						if(!tags[i].equals(value2)) {
							value3 = value3 + " " + tags[i] ;
						}
					}
					tb.setValue(value3.equals("")?"":value3.substring(1));
					lb.setState(0);
				}
				if(tb instanceof EnhancedTextBox) {
					((EnhancedTextBox)tb).update();
				}
			}
		});
	}
	
	public void updateTags(Tag[] tags) {
		for(int i = 0; i < tags.length; i++) {
			for(LabelWithLink l : lwl) {
				if(tags[i].getTagname().equals(l.getText())) {
					l.setFontSize(Util.getTagSize(tags[i]));
					break;
				}
			}
		}
	}
	
	public TextBox getPartner() {
		return tb;
	}
}
